package com.lixingye.meowstar.controller;

import com.lixingye.meowstar.common.AppResult;
import com.lixingye.meowstar.common.ResultCode;
import com.lixingye.meowstar.config.AppConfig;
import com.lixingye.meowstar.model.Message;
import com.lixingye.meowstar.model.User;
import com.lixingye.meowstar.service.IMessageService;
import com.lixingye.meowstar.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 猫大仙
 * @since 2025-01-14
 */
@Tag(name = "站内信接口")
@Slf4j
@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private IMessageService messageService;

    @Autowired
    private IUserService userService;
    @Operation(summary = "发送信息")
    @PostMapping("/send")
    public AppResult send(HttpServletRequest request,
                          @Parameter(description = "接收用户id") @RequestParam(value = "receiveUserId") @NonNull Long receiveUserId,
                          @Parameter(description = "内容") @RequestParam(value = "content") @NonNull String content){
        //当前用户是否被禁言
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() == 1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //不能给自己发
        if(receiveUserId == user.getId()){
            return AppResult.failed("不能给自己发送信息");
        }
        //接受者是否存在
        User recieveUser = userService.getById(receiveUserId);
        if( recieveUser == null || recieveUser.getDeleteState() == 1){
            return AppResult.failed("接收者状态异常");
        }
        Message message = new Message();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(receiveUserId);
        message.setContent(content);
        messageService.create(message);
        return AppResult.success("发送成功");
    }

    @Operation(summary = "未读信息数")
    @GetMapping("/getUnreadCount")
    public AppResult<Integer> getUnreadCount(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        return AppResult.success(messageService.selectUnreadCount(user.getId()));
    }

    @Operation(summary = "获取信息列表")
    @GetMapping("/getAll")
    public AppResult<List<Message>> getAll(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        List<Message> messages = messageService.selectByReceiveUserId(user.getId());
        return AppResult.success(messages);
    }
    @Operation(summary = "更新为已读")
    @PostMapping("/markRead")
    public AppResult markRead(HttpServletRequest request,
                              @Parameter(description = "站内信id") @RequestParam(value = "id") @NonNull Long id){
        //根据id查询站内信
        Message message = messageService.getById(id);
        //站内信是否存在
        if(message == null){
            return AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        //站内信是否是自己的
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(message.getReceiveUserId() != user.getId()){
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        messageService.updateStateById(id,(byte) 1);
        return AppResult.success("更新成功");
    }
    @Operation(summary = "回复站内信")
    @PostMapping("/reply")
    public AppResult reply(HttpServletRequest request,
                           @Parameter(description = "站内信id") @RequestParam(value = "repliedId") @NonNull Long repliedId,
                           @Parameter(description = "回复内容") @RequestParam(value = "content") @NonNull String content){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState() == 1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        Message exitmessage = messageService.getById(repliedId);
        if(exitmessage == null || exitmessage.getDeleteState() == 1){
            return AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        if(exitmessage.getPostUserId() == user.getId()){
            return AppResult.failed("不能给自己回复");
        }
        if(content == null){
            return AppResult.failed("回复内容不能为空");
        }
        Message message = new Message();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(exitmessage.getPostUserId());
        message.setContent(content);

        messageService.reply(repliedId,message);
        return AppResult.success("回复成功");
    }

}
